
local yichou = fk.CreateSkill {
  name = "hanqing__yichou",
  attached_skill_name = "hanqing__yichou&"
}

Fk:loadTranslationTable{
  ["hanqing__yichou"] = "夷雠",
  [":hanqing__yichou"] = "你可以将手牌调整为偶数并将半数非【影】手牌替换为【影】视为使用【闪】；"..
  "你与场上牌点数和最大的角色可以将【影】当【杀】使用，且以此法使用的【杀】仅能以上述方式响应。",

  ["#hanqing__yichou-jink"] = "夷雠：将%arg张非【影】手牌替换为%arg2张【影】，视为使用【闪】",
  ["#hanqing__yichou-slash"] = "夷雠：你可以将【影】当【杀】使用",
}

local U = require "packages/utility/utility"

yichou:addEffect("viewas", {
  mute = true,
  mute_card = false,
  pattern = "slash,jink",
  prompt = function (self, player, selected_cards, selected)
    if #player:getViewAsCardNames(yichou.name, {"slash"}) > 0 then
      return "#hanqing__yichou-slash"
    elseif #player:getViewAsCardNames(yichou.name, {"jink"}) > 0 then
      local n1 = (player:getHandcardNum() + 1) // 2
      local n2 = player:getHandcardNum() // 2
      return "#hanqing__yichou-jink:::"..n1..":"..n2
    end
  end,
  handly_pile = true,
  card_filter = function(self, player, to_select, selected)
    if #player:getViewAsCardNames(yichou.name, {"slash"}) > 0 then
      return #selected == 0 and Fk:getCardById(to_select).trueName == "shade"
    elseif #player:getViewAsCardNames(yichou.name, {"jink"}) > 0 then
      return table.contains(player:getCardIds("h"), to_select) and
        Fk:getCardById(to_select).trueName ~= "shade" and #selected * 2 < player:getHandcardNum()
    end
  end,
  view_as = function(self, player, cards)
    if #cards == 0 then return end
    if Fk:getCardById(cards[1]).trueName == "shade" then
      if #cards ~= 1 then return end
      local card = Fk:cloneCard("slash")
      card.skillName = yichou.name
      card:addSubcard(cards[1])
      return card
    else
      if #cards * 2 < player:getHandcardNum() then return end
      local card = Fk:cloneCard("jink")
      card.skillName = yichou.name
      card:addFakeSubcards(cards)
      return card
    end
  end,
  before_use = function (self, player, use)
    local room = player.room
    player:broadcastSkillInvoke(yichou.name)
    if use.card.trueName == "jink" then
      room:notifySkillInvoked(player, yichou.name, "defensive")
      local shade = U.getShade(room, player:getHandcardNum() // 2)
      local moves = {}
      table.insert(moves, {
        ids = use.card.fake_subcards,
        from = player,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonExchange,
        skillName = yichou.name,
        proposer = player,
        moveVisible = true,
      })
      table.insert(moves, {
        ids = shade,
        to = player,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonExchange,
        skillName = yichou.name,
        proposer = player,
        moveVisible = true,
      })
      room:moveCards(table.unpack(moves))
    else
      room:notifySkillInvoked(player, yichou.name, "offensive")
    end
  end,
  enabled_at_play = function (self, player)
    return #player:getViewAsCardNames(yichou.name, {"slash"}) > 0
  end,
  enabled_at_response = function (self, player, response)
    if response then return end
    if #player:getViewAsCardNames(yichou.name, {"slash"}) > 0 then
      return true
    elseif #player:getViewAsCardNames(yichou.name, {"jink"}) > 0 then
      local cards = table.filter(player:getCardIds("h"), function (id)
        return Fk:getCardById(id).trueName ~= "shade"
      end)
      return #cards * 2 >= player:getHandcardNum()
    end
  end,
})

yichou:addEffect(fk.HandleAskForPlayCard, {
  can_refresh = function(self, event, target, player, data)
    return data.eventData and data.eventData.from == player and data.eventData.card.trueName == "slash" and
      table.contains(data.eventData.card.skillNames, yichou.name)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if not data.afterRequest then
      room:setBanner(yichou.name, 1)
    else
      room:setBanner(yichou.name, 0)
    end
  end,
})

--此段用于绕过八卦阵
yichou:addEffect(fk.AskForCardUse, {
  can_refresh = function(self, event, target, player, data)
    return target == player and data.eventData and table.contains(data.eventData.card.skillNames, yichou.name)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setBanner(yichou.name, 1)
  end,
})
yichou:addEffect(fk.AskForCardUse, {
  late_refresh = true,
  can_refresh = function(self, event, target, player, data)
    return target == player and data.eventData and table.contains(data.eventData.card.skillNames, yichou.name)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setBanner(yichou.name, 0)
  end,
})

yichou:addEffect("prohibit", {
  prohibit_use = function(self, player, card)
    if card and Fk:currentRoom():getBanner(yichou.name) then
      return not table.contains(card.skillNames, yichou.name)
    end
  end,
})

--如果还是被绕过（eg.护驾），则康掉
yichou:addEffect(fk.PreCardEffect, {
  can_refresh = function(self, event, target, player, data)
    return target == player and data.card.name == "jink" and
      data.toCard and table.contains(data.toCard.skillNames, yichou.name) and
      not table.contains(data.card.skillNames, yichou.name)
    end,
  on_refresh = function (self, event, target, player, data)
    data.nullified = true
  end,
})

yichou:addAI(nil, "vs_skill")

return yichou
